//
//  TFmmonPhrasesView.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/5/6.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TFmmonPhrasesView: UIView,UICollectionViewDataSource,UICollectionViewDelegate {
    //点击了常用语
    @objc public var commonLanguageBlock:TF1to16ExpressionCellmyBlock?
  
    @objc public let commonPhrases = NSMutableArray()
    @objc public var collectionView:UICollectionView?
    override init(frame: CGRect) {
        super.init(frame: frame)
    
        let a1 =  "你能看到我画面吗"
        let a2 = "你能听到我声音吗"
        let a3 = "我听不到你的声音?"
        let a4 = "我听不到你的声音?"
        self.commonPhrases.add(a1)
        self.commonPhrases.add(a2)
        self.commonPhrases.add(a3)
        self.commonPhrases.add(a4)
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        if(self.frame.size.width>0&&self.frame.size.height>0){
            if (self.collectionView==nil ) {
            
                 let layout = UICollectionViewFlowLayout()
                 layout.itemSize = CGSize(width: 190, height: 44)
                 //垂直方向
                 layout.scrollDirection = .vertical
                // 平均后的间距
                let fSpacing:CGFloat = 6

                layout.sectionInset = UIEdgeInsets(top: fSpacing, left: 26, bottom: fSpacing, right: 26)
                
                self.collectionView = UICollectionView.init(frame: self.bounds, collectionViewLayout: layout)
                
                self.collectionView!.dataSource = self;
                self.collectionView!.delegate   = self;
                
                //注册Cell，必须要有
                self.collectionView?.register(TFCommonPhrasesCollectionViewCell.self, forCellWithReuseIdentifier: "TFCommonPhrasesCollectionViewCell")
            
                self.addSubview(self.collectionView!)
                
                self.collectionView!.backgroundColor = UIColor.clear
                
                if #available(iOS 11.0, *) {
                    
                    self.collectionView!.contentInsetAdjustmentBehavior = .never
                    
                }
            }else{
                self.collectionView!.frame = self.bounds
            }
            
            self.collectionView!.reloadData()

        }
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.commonPhrases.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TFCommonPhrasesCollectionViewCell", for: indexPath) as!TFCommonPhrasesCollectionViewCell
        
        let str = self.commonPhrases[indexPath.row] as! String
        cell.btn.setTitle(str, for: .normal)

        cell.myBlock = { [weak self] (str) in
            guard let `self` = self else { return }
            if (self.commonLanguageBlock != nil) {
                self.commonLanguageBlock!(str)
            }
        }
        
        
        
        return cell
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
